#include <bits/stdc++.h>

using namespace std;
const int N=4050;
int T,n,a[N],lb,res,s[N],ans;
int get(int l,int r){
    if(a[l]==a[l-1])return s[r]-s[l-1]-a[l];
    return s[r]-s[l];
}
int dfs(int x){
    if(x==n)ans=max(ans,res);
    for(int i=x+1;i<=n;i++){
        if(i==x+1){
            if(a[x]==a[x+1])res+=a[x];
            dfs(i);
            if(a[x]==a[x+1])res-=a[x];
        }else{
            if(lb!=0){
                int t=lb;
                if(a[x+1]==a[lb])res+=a[x+1];lb=i-1;
                res+=get(x+1,i-1);
                if(a[i]==a[x])res+=a[x];
                dfs(i);
                if(a[x+1]==a[lb])res-=a[x+1];lb=t;
                res-=get(x+1,i-1);
                if(a[i]==a[x])res-=a[x];
            }
        }
    }
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    cin>>T;
    while(T--){
        cin>>n;
        for(int i=1;i<=n;i++)cin>>a[i];
        lb=0;int cur=0;
        for(int i=1;i<=n;i++){
            if(a[i]==a[i-1])cur+=a[i];
            s[i]=cur;
        }
        dfs(1);
        cout<<ans<<endl;
        }
    return 0;
}
